package net.WMisiedjan.BukkitToDate.Launcher;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;

import org.apache.xbean.classloader.JarFileClassLoader;

public class CraftBukkitThread {
	
	public Thread mthread = null;
	public JarFileClassLoader jarClassLoader = null;
	public SecurityManager manager = new SecurityManager();
	public Class<?> c = null;
	public CraftBukkitThread() {
		
	}

	@SuppressWarnings("rawtypes")
	public void run() {
		try {
			   // Call here your lib code
			
		File CBF = null;

		try {
			CBF = new File(new File(".").getCanonicalPath()
					+ "\\craftbukkit.jar");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		if (CBF.exists()) {
			
			try {
				jarClassLoader = new JarFileClassLoader("craftbukkit.jar",new URL[] {CBF.toURI().toURL()});
			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
			//ClassLoader cl = Thread.currentThread().getContextClassLoader();
			
			//Thread.currentThread().setContextClassLoader(urlClassLoader); // switch to
			
			try {
				c = jarClassLoader.loadClass("org.bukkit.craftbukkit.Main");
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			String[] args = new String[] { "" };
			Class[] argTypes = new Class[] { String[].class };
		    Method main = null;
			try {
				main = c.getMethod("main", argTypes);
			} catch (SecurityException e1) {
				System.out.println("TEST123!");
				InputStreamReader converter = new InputStreamReader(System.in);
				BufferedReader in = new BufferedReader(converter);
				try {
					in.readLine();
				} catch (IOException e12) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			} catch (NoSuchMethodException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
	  	    String[] mainArgs = Arrays.copyOfRange(args, 1, args.length);
		    System.out.format("invoking %s.main()%n", c.getName());
		    try {
				main.invoke(null, (Object)mainArgs);
			} catch (IllegalArgumentException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (InvocationTargetException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch(ExitException e1) 
			{
				System.out.println("Event Cancelled 1!.");
					
					InputStreamReader converter = new InputStreamReader(System.in);
					BufferedReader in = new BufferedReader(converter);
					try {
						in.readLine();
					} catch (IOException e13) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
			}
			
			// do your stuff with the other jar
			//Thread.currentThread().setContextClassLoader(cl); // switch back to
																// the
																// original CL
		}
		} catch (SecurityException e) {
			  System.out.println("DEBUG [1]");
				InputStreamReader converter = new InputStreamReader(System.in);
				BufferedReader in = new BufferedReader(converter);
				try {
					in.readLine();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
	}

	public void Start() {
		this.run();
	}

	@SuppressWarnings("deprecation")
	public void Terminate() {
	}

}
